package code.ws_common_scene.command.cmd
{
	import code.ws_common_scene.GameScene;
	import code.ws_common_scene.command.Command;
	import code.ws_common_scene.manager.SceneManager;
	import code.ws_common_scene.mode.Hero;
	import code.ws_common_utils.MapUtils;
	
	import flash.geom.Point;

	/**
	 * 移动指令 
	 * @author qiangchen
	 * 
	 */	
	public class WalkCommand extends Command
	{
		/** 目标点 */
		private var tar_p:Point;
		
		/**
		 * 构造函数 
		 * @param tarP	像素点（localPoint）而不是stage全局坐标点
		 */		
		public function WalkCommand( tarP:Point )
		{
			super( null, false );
			tar_p = tarP;
		}
		
		
		/**
		 * 执行 
		 * 
		 */	
		override public function execute():void
		{
			var scene:GameScene = SceneManager.instance.scene;
			var hero:Hero = SceneManager.instance.scene.hero;
			var hero_p:Point = hero.tiles;
			tar_p = MapUtils.pixelToTile( tar_p, scene.mapData );
			if( hero_p.equals( tar_p ) )
			{
				hero = null;
				hero_p = null;
				scene = null;
				over();
				return;
			}
			
			var path:Array = scene.findPath( hero_p, tar_p );
			if( path == null || path.length == 0 )
			{
				// 路径不存在，或者路径长度为0
				hero = null;
				hero_p = null;
				scene = null;
				path = null;
				over();
				return;
			}
			
			hero.walk( path.slice() );
			
			// 释放引用对象
			scene = null;
			hero = null;
			hero_p = null;
			path.length = 0;
			path = null;
		}
		
		
		/**
		 * 指令结束 
		 * 
		 */		
		override public function over():void
		{
			tar_p = null;
			super.over();
		}
		
		
		/**
		 * 释放指令 
		 * 
		 */		
		override public function dispose():void
		{
			tar_p = null;
			super.dispose();
		}
	}
}